package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.UserRole;
import com.ebring.shareEducation.dao.UserRoleMapper;
import com.ebring.shareEducation.service.UserRoleService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Cola
 * Date  2018/8/20 22:40
 */

@Service
@Transactional
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    // 查询用户所有角色
    @Override
    public RspMsg<List<UserRole>> select(String userId) {
        RspMsg rspMsg = new RspMsg();
        List<UserRole> slist = userRoleMapper.selectById(userId);
        if (slist.isEmpty() || slist == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }

        rspMsg.setOK();
        rspMsg.setResult(slist);
        return rspMsg;
    }

    // 修改用户角色
    @Override
    public RspMsg<UserRole> update(String userId, String[] roleId) {
        RspMsg rspMsg = new RspMsg();
        int i = userRoleMapper.deletes(userId);
        if (i > 0) {
            int sum = 0;
            for (String s : roleId) {
                UserRole userRole = new UserRole();
                userRole.setId(UuidUtils.getUUID());
                userRole.setUserId(userId);
                userRole.setRoleId(s);
                userRoleMapper.insertSelective(userRole);
                sum++;
            }

            if (sum > 0) {
                rspMsg.setOK();
                return rspMsg;
            }
        }

        rspMsg.setError();
        rspMsg.setResult("更新角色失败");
        return rspMsg;
    }


}
